﻿using System;
using LockerRack.Helpers;
using LockerRack.Interfaces;
using Primitive.Mvvm.Framework.Implementations;
using Primitive.Mvvm.Framework.Interfaces;
using UIController.Models;

namespace DataController.ViewModels
{
    public class TaskDetailsViewModel : BaseViewModel
    {
        private ToDoModel _model;
        
        public DateTime MinDate { get; private set; }
        public DateTime MaxDate { get; private set; }
        
        public TaskDetailsViewModel(IModel model, IMediator mediator) : base(mediator)
        {
            _model = model as ToDoModel;
            
            MinDate = DateTime.Now;
            MaxDate = new DateTime(MinDate.Year + 1, MinDate.Month, MinDate.Day);
        }

        public IDrawerFile SelectedFile
        {
            get { return _model.With(m => m.SelectedFile); }
        }

        public DateTime TaskIssueDate
        {
            get
            {
                var bag = SelectedFile.With(f => f.Contents).Return(c => c.PropertyBag, null);

                if (PropertyExists(bag, "IssueDate"))
                {
                    var date = bag.IssueDate;
                    if (date < MinDate)
                        return MinDate;
                    if(date > MaxDate)
                        return MaxDate;
                    
                    return date;
                }

                return DateTime.Now;
            }
            set
            {
                if (SelectedFile == null || SelectedFile.Contents == null)
                    return;

                if (PropertyExists(SelectedFile.Contents.PropertyBag, "IssueDate"))
                {
                    SelectedFile.Contents.PropertyBag.IssueDate = value;
                    RaisePropertyChanged(this, () => TaskIssueDate);
                }
            }
        }

        public override void NotifyViewModelChanged(object sender, System.ComponentModel.PropertyChangedEventArgs pce)
        {
            base.NotifyViewModelChanged(sender, pce);
        }
    }
}
